package doyouknow.group.CnAndZhb.controller;

import com.alibaba.fastjson.JSONObject;
import doyouknow.group.CnAndZhb.core.IMQTTPublisher;
import doyouknow.group.CnAndZhb.core.IMQTTSubscriber;
import doyouknow.group.CnAndZhb.core.MQTTConfig;
import doyouknow.group.CnAndZhb.core.MQTTPublisher;
import doyouknow.group.CnAndZhb.entity.TblGateway;
import doyouknow.group.CnAndZhb.response.BaseControllerReturn;
import doyouknow.group.CnAndZhb.service.TblGatewayService;
import doyouknow.group.CnAndZhb.utils.DateUtil;
import doyouknow.group.CnAndZhb.utils.SerializedField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@RestController
public class DemoRestController extends BaseController{
    //    public static String TOPIC_LOOP_TEST = "mqtt/loop/message";
    public static String TOPIC_LOOP_TEST = "hotel_gateway";
    public static String TOPIC_PUBLISH = "hotel_sub_gateway002";

    @Autowired
    IMQTTPublisher publisher;



    @Autowired
    IMQTTSubscriber subscriber;

    @Autowired
    TblGatewayService tblGatewayService;



    /**
     * 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器执行一次。
     * PostConstruct在构造函数之后执行，init（）方法之前执行。PreDestroy（）方法在destroy（）方法知性之后执行
     * 这里初始化订阅一个主题
     * 这个注释要划重点划重点划重点划重点划重点
     * 所以你想在订阅了某个mqttServer之后还去更改mqttServer来发送消息吗？
     */
    @PostConstruct
    public void init() {
        subscriber.subscribeMessage(TOPIC_LOOP_TEST);
    }


    /**
     * 向指定主题发送消息
     *
     * @param data 数据
     * @return 响应
     */
    @RequestMapping(value = "/mqtt/push/message", method = RequestMethod.POST)
    @SerializedField(enable = false)
    public Object index(@RequestBody String data) {
        System.out.println(data);
        publisher.publishMessage(TOPIC_PUBLISH, data);
        return BaseControllerReturn.create(new HashMap<String,String>(){{
            put("msg","打开入网模式成功");
        }},null);
    }


    @RequestMapping("/mqtt/push")
    @SerializedField(enable = false)
    /**
     * 根据网关号查询mqttServer,mqttPort,mqttSubTopic
     * @param data 数据
     * @return 响应
     */
    public Object OpenJoin(@RequestBody JSONObject requestJson){

        String mac = requestJson.getString("GatewayMac");
        String service = requestJson.getString("Service");


        TblGateway tblGateway = new TblGateway();
        tblGateway = this.tblGatewayService.queryByMac(mac);
        String mqttServer = tblGateway.getMqttServer();
        String mqttPort = tblGateway.getMqttPort();
        Integer mqttPortInt = Integer.parseInt(mqttPort);
        String mqttSubTopic = tblGateway.getMqttSubTopic();
        Boolean ssl = true;

        JSONObject data = new JSONObject();
        data.put("GatewayMac", mac);
        data.put("UpdateTime", DateUtil.getNow());
        data.put("Service", service);

        publisher.publishMessage(mqttSubTopic, data.toJSONString());


        return BaseControllerReturn.create(new HashMap<String,String>(){{
            put("msg","打开入网模式成功");
        }},null);
    }



}
